package cn.ven.config;

import cn.ven.entity.MyUser;
import cn.ven.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity  //过滤器
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired(required = false)
    UserMapper userMapper;

    @Override
    public void configure(WebSecurity web) throws Exception {
        // 将 check_token 暴露出去，否则资源服务器访问时报 403 错误
        web.ignoring().antMatchers("/oauth/check_token");
        //放行静态资源
        web.ignoring().antMatchers("/static/**");
        web.ignoring().antMatchers("/css/**");
        web.ignoring().antMatchers("/js/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf()
                .disable()
                .authorizeRequests()
                .antMatchers("/**").permitAll()
                .anyRequest()
                .permitAll()
                .and()
                .formLogin()
                .permitAll();

        http.httpBasic().disable();
    }

      //client_id  client_secret
    //密码比对器
    @Bean("authenticationManagerBean")
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }



    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    // t_user 用户信息
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //使用匿名内部类
        auth.userDetailsService(new UserDetailsService() {
                                    @Override
                                    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
                                        MyUser myUser = userMapper.findUserByAccount(username);
                                        return myUser;
                                    }
                                });
    }
}
